package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.base.BaseController;
import com.shop.entity.User;
import com.shop.result.ResultVO;
import com.shop.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author XCH
 * @date 2025 - 05 - 19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user")
public class UserController extends BaseController {
    private final UserService userService;

    /**
     * 查询用户列表
     * @param pageNO
     * @param pageSize
     * @return
     */
    @GetMapping("/listUser")
    public ResultVO<PageInfo<User>> listUser(@RequestParam("pageNo") Integer pageNO, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<User> list = userService.listUser(pageNO, pageSize);
        return success(list);
    }
    /**
     * 用户登录
     *
     * @param username 用户名
     * @param password 密码
     * @return 用户信息
     */
    @PostMapping("/userLogin")
    public ResultVO<User> userLogin(@RequestParam("username") String username, @RequestParam("password") String password) {
        User user = userService.userLogin(username, password);
        if (username == null && password == null ) {
            return error("用户名或密码错误");
        }
        if (user == null) {
            return error("用户名或密码错误");
        }
        return success(user);
    }

    /**
     * 查询用户通过邮箱
     * @param email
     * @return
     */
    @GetMapping("/selectByEmail")
    public ResultVO<User> selectByEmail(@RequestParam("email") String email) {
        return success(userService.selectByEmail(email));
    }
    /**
     * 添加用户
     * @param user 用户信息
     */

    @PostMapping("/addUser")
    public ResultVO<User> addUser( User user) {
        userService.addUser(user);
        return success(user);
    }

    /**
     * 删除用户
     * @param id 用户id
     */
    @PostMapping("/deleteUser")
    public ResultVO<Object> deleteUser(@RequestParam("id") Integer id) {
        userService.deleteUser(id);
        return success();
    }

    /**
     * 修改用户
     * @param user 用户信息
     */
    @PostMapping("/updateUser")
    public ResultVO<User> updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return success(user);
    }

    /**
     * 通过id查询用户
     * @param id 用户id
     * @return 用户信息
     */
    @GetMapping("/SelectByUserId")
    public ResultVO<User> SelectByUserId(@RequestParam("id")Integer id) {
        return success(userService.SelectByUserId(id));
    }

    /**
     * 查询用户数量
     * @return 用户数量
     */
    @GetMapping("/countUser")
    public ResultVO<Integer> countUser() {
        return success(userService.countUser());
    }

    /**
     * 通过用户名查询用户
     * @param username
     * @return
     */
    @GetMapping("/selectByUsername")
    public ResultVO<User>  selectByUsername(@RequestParam("username") String username) {
        return success(userService.selectByUsername(username));

    }

    /**
     * 修改用户密码
     * @param id
     * @param password
     */
    @PostMapping("/updateUserPassword")
    public ResultVO<User> updateUserPassword(@RequestParam("id")Integer id, @RequestParam("password")String password) {
        userService.updateUserPassword(id, password);
        return success(200, "修改成功");
    }


    @PostMapping("/updateUserPhone")
    public ResultVO<User> updateUserPhone(@RequestParam("id") Integer id, @RequestParam("phone") String phone) {
        userService.updateUserPhone(id, phone);
        return success(200, "修改成功");
    }
    @PostMapping("/updateImage")
    public ResultVO<User> updateImage(@RequestParam("id") Integer id, @RequestParam("image") String image){
        userService.updateImage(id, image);
        return success(200, "修改成功");
    }
    @PostMapping("/updateNickname")
    public ResultVO<User> updateNickname(@RequestParam("id") Integer id, @RequestParam("nickname") String nickname){
        userService.updateNickname(id, nickname);
        return success(200, "修改成功");
    }
    @PostMapping("/updateEmail")
    public ResultVO<User> updateEmail(@RequestParam("id") Integer id, @RequestParam("email") String email){
        userService.updateEmail(id, email);
        return success(200, "修改成功");
    }
    @PostMapping("/updateSex")
    public ResultVO<User> updateSex(@RequestParam("id") Integer id, @RequestParam("sex") String sex){
        userService.updateSex(id, sex);
        return success(200, "修改成功");
    }
    @PostMapping("/updateUsername")
    public ResultVO<User> updateUsername(@RequestParam("id") Integer id, @RequestParam("username") String username){
        userService.updateUsername(id, username);
        return success(200, "修改成功");
    }

}
